﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ExSystem.ExIO
{
    public class ExFile
    {
        /// <summary>
        /// 读出文件的所有内容,任何出错都会返回空字符串。
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static string ReadToEnd(string filePath)
        {
            return ReadToEnd(filePath, Encoding.Default);

        }

        public static string ReadToEnd(string filePath,Encoding encode)
        {
            string a = "";

            if (File.Exists(filePath))
            {

                StreamReader r = new StreamReader(filePath, encode);
                try
                {
                    a = r.ReadToEnd();
                }
                catch (Exception e) { }
                finally { r.Close(); }
                
            }

            return a;

        }

        public static bool Save(string data, string filePath,Encoding encode)
        {
         
            try
            {
                if (File.Exists(filePath)) File.Delete(filePath);
                StreamWriter w = new StreamWriter(File.Create(filePath), encode);
                w.Write(data);
                w.Flush();
                w.Close();
                return true;
            }
            catch (Exception e) {};
            return false;
        }
        public static bool Save(string data, string filePath)
        {
            return Save(data, filePath, Encoding.Default);
        }
    }
}
